package com.ydgk.dao;

import com.ydgk.bean.Teacher;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.List;

public class TeacherDao extends BaseDao<Teacher> {
    @Override
    public int save(Teacher teacher) {
        return template.update("insert into teacher values(?,?,?,?,?)",
                teacher.getTid(),
                teacher.getTname(),
                teacher.getTsal(),
                teacher.getTsex(),
                teacher.getTdate());
    }

    @Override
    public int updateById(Teacher teacher) {
        return template.update("update teacher set tname = ?,tsal = ?,tsex = ?,tdate = ? where tid = ?",
                teacher.getTname(),
                teacher.getTsal(),
                teacher.getTsex(),
                teacher.getTdate(),
                teacher.getTid());
    }

    @Override
    public int deleteById(int id) {
        try {
            return template.update("delete from teacher where tid = ?", id);
        }catch (Exception e){
            return -1;
        }
    }

    @Override
    public List<Teacher> findAll() {
        return template.query("select *from teacher",new BeanPropertyRowMapper<Teacher>(Teacher.class));
    }

    @Override
    public Teacher findById(int id) {
        String sql = "select * from teacher where tid =?" ;
        List<Teacher> list = template.query(sql, new BeanPropertyRowMapper<Teacher>(Teacher.class),id);
        return list.size() > 0 ? list.get(0) : null ;
    }

    @Override
    public List<Teacher> paging(int pageNumber) {
        return template.query("select * from teacher order by tid desc limit ?,?",new BeanPropertyRowMapper<Teacher>(Teacher.class) , (pageNumber-1)*pageCount ,pageCount);
    }

    public int total() {
        return template.queryForObject("select count(*) from teacher",Integer.class);
    }
}
